<?php
defined('IN_PHPCMS') or exit('No permission resources.');
class Fusion_Image {
    private $FC_Color_Counter = 0;
    private $Array_FC_Color = array(
        "FF0000",//Dark Blue
        "AFD8F8",
        "F6BD0F",
        "8BBA00",
        "A66EDD",
        "F984A1",
        "CCCC00",//Chrome Yellow+Green
        "999999",//Grey
        "0099CC",//Blue Shade
        "FF0000",//Bright Red 
        "006F00",//Dark Green
        "0099FF",//Blue (Light)
        "FF66CC",//Dark Pink
        "669966",//Dirty green
        "7C7CB4",//Violet shade of blue
        "FF9933",//Orange
        "9900FF",//Violet
        "99FFCC",//Blue+Green Light
        "CCCCFF",//Light violet
        "669900",//Shade of green
    );

    private $url = "";
    private $XML = "";
    private $data_array = array();
    private $image_title = "";
    public $number_prefix = "";

    function __construct($image_title, $data_array) {
        $this->image_title = $image_title;
        $this->data_array = $data_array;

        $this->generate_XML();
    } 

    function __destruct() {
    } 

    private function generate_XML() {
        $strXML = "<graph caption='$this->image_title' numberPrefix='$this->number_prefix' 
            formatNumberScale='1' decimalPrecision='2' >";
        //Convert data to XML and append
        foreach ($this->data_array as $arSubTitle => $arSubData) {
            $strXML .= "<set name='" . $arSubTitle . "' value='" . $arSubData . "' color='" . $this->getFCColor() . "' />";
        }

        //Close <graph> element
        $strXML .= "</graph>";
        $this->XML = $strXML;
        //return $strXML;
    } 

    private function getFCColor() {
        //Update index
        $this->FC_Color_Counter++;
        //Return color
        return $this->Array_FC_Color[$this->FC_Color_Counter % count($this->Array_FC_Color)];
    } 

    // boolToNum function converts boolean values to numeric (1/0)
    private function boolToNum($bVal) {
        return ($bVal==true) ? 1 : 0;
    }

    public function renderChartHTML($chartSWF, $chartId = "", $chartWidth = "500", $chartHeight = "300") {
        // Generate the FlashVars string based on whether dataURL has been provided
        // or dataXML.
        $strFlashVars = "&chartWidth=" . $chartWidth . "&chartHeight=" . $chartHeight ;
        if ($this->XML=="")
            // DataURL Mode
            $strFlashVars .= "&dataURL=" . $this->url;
        else
            //DataXML Mode
            $strFlashVars .= "&dataXML=" . $this->XML;

        $HTML_chart = <<<HTMLCHART
    <!-- START Code Block for Chart $chartId -->
    <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"  width="$chartWidth" height="$chartHeight" id="$chartId">
        <param name="allowScriptAccess" value="always" />
        <param name="movie" value="$chartSWF"/>		
        <param name="FlashVars" value="$strFlashVars" />
        <param name="quality" value="high" />
        <embed src="$chartSWF" FlashVars="$strFlashVars" quality="high" width="$chartWidth" height="$chartHeight" name="$chartId" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
    </object>
    <!-- END Code Block for Chart $chartId -->
HTMLCHART;

        return $HTML_chart;
    }

    public function renderChart($chartSWF, $chartId = "", $chartWidth = "500", $chartHeight = "300") {
        if ($this->XML=="")
            $tempData = "//Set the dataURL of the chart\n\t\tchart_$chartId.setDataURL(\"$this->url\")";
        else
            $tempData = "//Provide entire XML data using dataXML method\n\t\tchart_$chartId.setDataXML(\"$this->XML\")";

        // Set up necessary variables for the RENDERCAHRT
        $chartIdDiv = $chartId . "Div";

        // create a string for outputting by the caller
        $render_chart = <<<RENDERCHART
    <!-- START Script Block for Chart $chartId -->
    <div id="$chartIdDiv" align="center">
        Chart.
    </div>
    <script type="text/javascript">	
        //Instantiate the Chart	
        var chart_$chartId = new FusionCharts("$chartSWF", "$chartId", "$chartWidth", "$chartHeight");
        $tempData
        //Finally, render the chart.
        chart_$chartId.render("$chartIdDiv");
    </script>	
    <!-- END Script Block for Chart $chartId -->
RENDERCHART;

        return $render_chart;
    }

    // encodeDataURL function encodes the dataURL before it's served to FusionCharts.
    // If you've parameters in your dataURL, you necessarily need to encode it.
    // Param: $strDataURL - dataURL to be fed to chart
    // Param: $addNoCacheStr - Whether to add aditional string to URL to disable caching of data
    function encodeDataURL($strDataURL, $addNoCacheStr=false) {
        //Add the no-cache string if required
        if ($addNoCacheStr==true) {
            // We add ?FCCurrTime=xxyyzz
            // If the dataURL already contains a ?, we add &FCCurrTime=xxyyzz
            // We replace : with _, as FusionCharts cannot handle : in URLs
            if (strpos(strDataURL,"?")<>0)
                $strDataURL .= "&FCCurrTime=" . Date("H_i_s");
            else
                $strDataURL .= "?FCCurrTime=" . Date("H_i_s");
    }
        // URL Encode it
        return urlencode($strDataURL);
    }

    // datePart function converts MySQL database based on requested mask
    // Param: $mask - what part of the date to return "m' for month,"d" for day, and "y" for year
    // Param: $dateTimeStr - MySQL date/time format (yyyy-mm-dd HH:ii:ss)
    function datePart($mask, $dateTimeStr)
    {
        @list($datePt, $timePt) = explode(" ", $dateTimeStr);
        $arDatePt = explode("-", $datePt);
        $dataStr = "";
        // Ensure we have 3 parameters for the date
        if (count($arDatePt) == 3) {
            list($year, $month, $day) = $arDatePt;
            // determine the request
            switch ($mask) {
            case "m": return (int)$month;
            case "d": return (int)$day;
            case "y": return (int)$year;
            }
            // default to mm/dd/yyyy
            return (trim($month . "/" . $day . "/" . $year));
        }
        return $dataStr;
    }
}
?>
